package logic

import (
	"context"
	"errors"
	"house/cache"
	rand2 "math/rand"
	"time"

	"house/house"
	"house/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendSmsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSendSmsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendSmsLogic {
	return &SendSmsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SendSmsLogic) SendSms(in *house.SendSmsRequest) (*house.SendSmsResponse, error) {
	// todo: add your logic here and delete this line
	result, _ := cache.Rdb.Get(l.ctx, "SendSms"+in.Mobile).Result()
	if result != "" {
		logx.Error("五分钟只能发送一次")
		return nil, errors.New("五分钟只能发送一次")
	}
	code := rand2.Intn(9000) + 1000
	//sms, err := pkg.SendSms(in.Mobile, strconv.Itoa(code))
	//if err != nil {
	//	return nil, err
	//}
	//if *sms.Body.Code == "OK" {
	//	logx.Error(*sms.Body.Message)
	//	return nil, errors.New(*sms.Body.Message)
	//}
	cache.Rdb.Set(l.ctx, "SendSms"+in.Mobile, code, 0)
	cache.Rdb.Incr(l.ctx, "SendSmsErr"+in.Mobile)
	cache.Rdb.Expire(l.ctx, "SendSms"+in.Mobile, time.Minute*5)
	return &house.SendSmsResponse{}, nil
}
